package com.fitns.user.controller.baseConfig;

import com.alibaba.fastjson.JSON;
import com.fitns.user.conf.ExcelConstants;
import com.fitns.user.conf.Resp;
import com.fitns.user.dto.baseConfig.WorkPositionDTO;
import com.fitns.user.dto.baseConfig.WorkPositionVO;
import com.fitns.user.model.baseConfig.TbWorkPosition;
import com.fitns.user.service.baseConfig.WorkPositionService;
import com.fitns.user.utils.ExcelUtils;
import com.fitns.user.utils.ResultData;
import com.github.pagehelper.PageInfo;
import com.xiaoleilu.hutool.util.CollectionUtil;
import lombok.Synchronized;
import oracle.jrockit.jfr.StringConstantPool;
import org.springframework.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 *Created by ye on 2018/8/1
 *
 */

@RestController
@CrossOrigin //解决前后端对接跨域问题
@RequestMapping("/workPosition")
@Api(description = "作业区域")
public class WorkPositionController {

    @Autowired
    private WorkPositionService workPositionService;

    @PostMapping("/insert")
    @ApiOperation("新增")
    public ResultData insert(@RequestBody WorkPositionDTO workPositionDTO){
        WorkPositionVO workPositionVO = workPositionService.insert(workPositionDTO);
        return new ResultData(200,"SUCESS",workPositionVO,"");
    }

    @PutMapping("/modify")
    @ApiOperation("修改")
    public ResultData modify(@RequestBody WorkPositionDTO workPositionDTO){
        WorkPositionVO workPositionVO = workPositionService.modify(workPositionDTO);
        if(StringUtils.isEmpty(workPositionVO)){
            return new ResultData( 300,"DEFAULT",workPositionVO,"");
        }
        return new ResultData(200,"SUCESS",workPositionVO,"");
   }

    @GetMapping("/select")
    @ApiOperation("查询列表")
    public Object select( TbWorkPosition tbWorkPosition){
        PageInfo<WorkPositionVO> workPositionVOList= workPositionService.select(tbWorkPosition);
        workPositionVOList.setPageSize(tbWorkPosition.getPageSize());
        return Resp.page(workPositionVOList);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除")
    public ResultData delete(@RequestParam("id") Long id){
        workPositionService.delete(id);
        return new ResultData(200,"SUCESS",null,"删除成功");
    }

    @PostMapping(value = "/importData", produces = {"text/html;charset=UTF-8"})
    @Synchronized
    @ApiOperation("导入区域/路段数据")
    public String importData(@RequestParam("file") MultipartFile file, String redisKey, @RequestParam("organId") Long organId, @RequestParam("type") String type) throws Exception {
        List<List<String>> excelList=ExcelUtils.importExcel(file);
        List<String> headList=excelList.get(0);
        List<WorkPositionDTO> workPositionDTOS=new ArrayList<>();
        boolean is=ExcelConstants.WORK_POSITION_IMPORT_HEADER.XUHAO.equals(headList.get(0));
        if(ExcelConstants.WORK_POSITION_IMPORT_HEADER.XUHAO.equals(headList.get(0))&&
                ExcelConstants.WORK_POSITION_IMPORT_HEADER.NAME.equals(headList.get(1))&&
                ExcelConstants.WORK_POSITION_IMPORT_HEADER.PHONES.equals(headList.get(2))&&
                ExcelConstants.WORK_POSITION_IMPORT_HEADER.CARDS.equals(headList.get(3))&&
                ExcelConstants.WORK_POSITION_IMPORT_HEADER.ACREAGE.equals(headList.get(4))&&
                ExcelConstants.WORK_POSITION_IMPORT_HEADER.DESCS.equals(headList.get(5))&&
                ExcelConstants.WORK_POSITION_IMPORT_HEADER.POSITION.equals(headList.get(6))){
            workPositionDTOS=workPositionService.importExcel(excelList,organId,type);
        }else {
            WorkPositionDTO workPositionDTO=new WorkPositionDTO();
            workPositionDTO.setMsg("模板有误，请勿修改模板！");
            workPositionDTOS.add(workPositionDTO);
        }

        ResultData resultData=new ResultData();
        String errorData="";
        if (CollectionUtil.isNotEmpty(workPositionDTOS)) {
            errorData = JSON.toJSONString(workPositionDTOS);
        }
        if(org.apache.commons.lang3.StringUtils.isEmpty(errorData)){
            resultData.setMsg("成功");
        }else{
            resultData.setCode(300);
            resultData.setMsg("失败");
        }

        resultData.setObj(errorData);
        return JSON.toJSONString(resultData);
    }

}
